package config

import "github.com/spf13/viper"

type ServerConfig struct {
	Port int `yaml:"port"`
}

type MySqlConfig struct {
	Host            string `yaml:"host"`
	Port            int    `yaml:"port"`
	UserName        string `yaml:"userName"`
	PassWord        string `yaml:"passWord"`
	DataBase        string `yaml:"dataBase"`
	Charset         string `yaml:"charset"`
	ParseTime       string `yaml:"parseTime"`
	Local           string `yaml:"local"`
	MaxIdleConns    int    `yaml:"maxIdleConns"`
	MaxOpenConns    int    `yaml:"maxOpenConns"`
	ConnMaxLifetime int    `yaml:"connMaxLifetime"`
}

type Configuration struct {
	Server ServerConfig `yaml:"server"`
	Mysql  MySqlConfig  `yaml:"mysql"`
}

var (
	configuration *Configuration
)

// 从配置文件读取配置
func ReadConfiguration(configPath string) error {
	v := viper.New()
	v.SetConfigFile(configPath)
	v.SetConfigType("yaml")
	if err := v.ReadInConfig(); err != nil {
		return err
	}
	var config = Configuration{}
	if err := v.Unmarshal(&config); err != nil {
		return err
	}
	configuration = &config
	return nil
}

func GetConfiguration() *Configuration {
	return configuration
}
